import request from '@/utils/request'
import * as mock from './mock/lucky_draw'

const isMock = import.meta.env.VITE_API_MOCK === 'true'

/**
 * 获取用户抽奖信息
 * @returns {Promise} 返回用户抽奖信息，包括剩余次数、累计获得等
 */
export function getUserLuckyDrawInfo() {
  if (isMock) {
    return Promise.resolve(mock.getUserLuckyDrawInfo())
  }
  return request({
    url: '/lucky_draw/user_info',
    method: 'get'
  })
}

/**
 * 获取奖品列表
 * @returns {Promise} 返回奖品列表及概率
 */
export function getPrizeList() {
  if (isMock) {
    return Promise.resolve(mock.getPrizeList())
  }
  return request({
    url: '/lucky_draw/prizes',
    method: 'get'
  })
}

/**
 * 执行抽奖
 * @returns {Promise} 返回抽奖结果
 */
export function drawPrize() {
  if (isMock) {
    return Promise.resolve(mock.drawPrize())
  }
  return request({
    url: '/lucky_draw/draw',
    method: 'post'
  })
}

/**
 * 领取奖品
 * @param {Object} data 奖品信息
 * @param {number} data.prizeId 奖品ID
 * @param {number} data.recordId 抽奖记录ID
 * @returns {Promise} 返回领取结果
 */
export function claimPrize(data) {
  if (isMock) {
    return Promise.resolve(mock.claimPrize(data))
  }
  return request({
    url: '/lucky_draw/claim',
    method: 'post',
    data
  })
}

/**
 * 获取中奖记录
 * @param {Object} params 查询参数
 * @param {number} params.page 页码
 * @param {number} params.pageSize 每页数量
 * @returns {Promise} 返回中奖记录列表
 */
export function getPrizeRecords(params) {
  if (isMock) {
    return Promise.resolve(mock.getPrizeRecords(params))
  }
  return request({
    url: '/lucky_draw/records',
    method: 'get',
    params
  })
}